home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Commun⁄Network / NewsWatcher 2.0d17 Source / source / log.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-10  |  16.9 KB  |  673 lines  |  [TEXT/KAHL]

  1. /*----------------------------------------------------------------------------
  2.  
  3.     log.c
  4.  
  5.     This module handles logging.
  6.     
  7.     Portions copyright © 1990, Apple Computer.
  8.     Portions copyright © 1993, Northwestern University.
  9.  
  10. ----------------------------------------------------------------------------*/
  11.  
  12. #include <Packages.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15.  
  16. #include "glob.h"
  17. #include "log.h"
  18. #include "util.h"
  19. #include "dlgutil.h"
  20.  
  21.  
  22.  
  23. static short gRefNum = 0;
  24.  
  25.  
  26.  
  27. /*----------------------------------------------------------------------------
  28.     Munge
  29.     
  30.     Makes CR and LF characters visible in a relocatble block.
  31.     
  32.     Entry:    h = handle to block.
  33. ----------------------------------------------------------------------------*/
  34.  
  35. static void Munge (Handle h)
  36. {
  37.     long offset;
  38.     
  39.     for (offset = 0; offset >= 0; offset = Munger(h, offset, CRSTR, 1, "\\n", 2));
  40.     for (offset = 0; offset >= 0; offset = Munger(h, offset, LFSTR, 1, "\\r\r", 3));
  41. }
  42.  
  43.  
  44.  
  45. /*----------------------------------------------------------------------------
  46.     PutString 
  47.     
  48.     Writes a string to the log file.
  49.     
  50.     Entry:    str = string to write.
  51. ----------------------------------------------------------------------------*/
  52.  
  53. static void PutString (char *str)
  54. {
  55.     char line[513];
  56.     long len;
  57.     
  58.     sprintf(line, "%s\r", str);
  59.     len = strlen(line);
  60.     FSWrite(gRefNum, &len, line);
  61. }
  62.  
  63.  
  64.  
  65. /*----------------------------------------------------------------------------
  66.     LogBatchGroupCommandSend 
  67.     
  68.     Logs the batch sending of GROUP commands when checking for new
  69.     articles.
  70.     
  71.     Entry:    groupCmds = GROUP commands as sent to server.
  72.             groupCmdsLen = length of groupCmds buffer.
  73.             numGroupsToCheck = number of GROUP commands sent.
  74. ----------------------------------------------------------------------------*/
  75.  
  76. void LogBatchGroupCommandSend (char *groupCmds, short groupCmdsLen, short numGroupsToCheck)
  77. {
  78.     Handle tmpHandle;
  79.     long len;
  80.     CStr255 msg;
  81.  
  82.     if (gRefNum == 0) return;
  83.     PutString("");
  84.     sprintf(msg, "Batch GROUP commands sent to news server.\r%d commands sent.", numGroupsToCheck);
  85.     PutString(msg);
  86.     PtrToHand(groupCmds, &tmpHandle, groupCmdsLen);
  87.     Munge(tmpHandle);
  88.     len = GetHandleSize(tmpHandle);
  89.     HLock(tmpHandle);
  90.     FSWrite(gRefNum, &len, *tmpHandle);
  91.     MyDisposHandle(tmpHandle);
  92. }
  93.  
  94.  
  95.  
  96. /*----------------------------------------------------------------------------
  97.     LogBatchGroupCommandResponse 
  98.     
  99.     Logs a buffer received from the server in response to the batch sending 
  100.     of GROUP commands when checking for new articles.
  101.     
  102.     Entry:    response = buffer received from server.
  103.             length = length of buffer.
  104.             numReceived = number of lines received from server so far.
  105. ----------------------------------------------------------------------------*/
  106.  
  107. void LogBatchGroupCommandResponse (char *response, long length, short numReceived)
  108. {
  109.     Handle tmpHandle;
  110.     long len;
  111.     CStr255 msg;
  112.  
  113.     if (gRefNum == 0) return;
  114.     PutString("");
  115.     PutString("Buffer received from news server in response to batch GROUP commands.");
  116.     sprintf(msg, "%d responses received so far.", numReceived);
  117.     PutString(msg);
  118.     PtrToHand(response, &tmpHandle, length);
  119.     Munge(tmpHandle);
  120.     len = GetHandleSize(tmpHandle);
  121.     HLock(tmpHandle);
  122.     FSWrite(gRefNum, &len, *tmpHandle);
  123.     if ((*tmpHandle)[len-1] != '\r') {
  124.         len = 1;
  125.         FSWrite(gRefNum, &len, CRSTR);
  126.     }
  127.     MyDisposHandle(tmpHandle);
  128. }
  129.  
  130.  
  131.  
  132. /*----------------------------------------------------------------------------
  133.     LogBatchGroupCommandsFinished 
  134.     
  135.     Logs the completion of a batch GROUP command send and receive operation.
  136. ----------------------------------------------------------------------------*/
  137.  
  138. void LogBatchGroupCommandsFinished (void)
  139. {
  140.     if (gRefNum == 0) return;
  141.     PutString("");
  142.     PutString("All news server responses successfully received from batch GROUP commands.");
  143. }
  144.  
  145.  
  146.  
  147. /*----------------------------------------------------------------------------
  148.     LogGroupCommand 
  149.     
  150.     Log a GROUP command.
  151.     
  152.     Entry:    groupCmd = pointer to GROUP command, no CRLF terminator,
  153.                 0 terminator.
  154. ----------------------------------------------------------------------------*/
  155.  
  156. void LogGroupCommand (char *groupCmd)
  157. {
  158.     char msg[512];
  159.  
  160.     if (gRefNum == 0) return;
  161.     PutString("");
  162.     sprintf(msg, "GROUP command sent to server: %s", groupCmd);
  163.     PutString(msg);
  164. }
  165.  
  166.  
  167.  
  168. /*----------------------------------------------------------------------------
  169.     LogGroupCommandResponse 
  170.     
  171.     Log a response to a GROUP command.
  172.     
  173.     Entry:    response = pointer to response, with CRLF terminator.
  174.             length = length of response.
  175. ----------------------------------------------------------------------------*/
  176.  
  177. void LogGroupCommandResponse (char *response, unsigned short length)
  178. {
  179.     CStr255 resp;
  180.     char msg[512];
  181.     char *p, *pEnd;
  182.     
  183.     if (gRefNum == 0) return;
  184.     if (length > 255) length = 255;
  185.     pEnd = response + length;
  186.     for (p = response; *p != CR && p < pEnd; p++);
  187.     length = p - response;
  188.     BlockMove(response, resp, length);
  189.     resp[length] = 0;
  190.     sprintf(msg, "GROUP command response received: %s", resp);
  191.     PutString(msg);
  192. }
  193.  
  194.  
  195.  
  196. /*----------------------------------------------------------------------------
  197.     LogGetGroupArrayArticleRangesOneAtATimeBegin 
  198.     
  199.     Log the begin of a get group array article range one at a time operation.
  200. ----------------------------------------------------------------------------*/
  201.  
  202. void LogGetGroupArrayArticleRangesOneAtATimeBegin (void)
  203. {
  204.     if (gRefNum == 0) return;
  205.     PutString("");
  206.     PutString("Begin get group array article range one a time operation.");
  207. }
  208.  
  209.  
  210.  
  211. /*----------------------------------------------------------------------------
  212.     LogGetGroupArrayArticleRangesOneAtATimeEnd 
  213.     
  214.     Log the end of a get group array article range one at a time operation.
  215. ----------------------------------------------------------------------------*/
  216.  
  217. void LogGetGroupArrayArticleRangesOneAtATimeEnd (void)
  218. {
  219.     if (gRefNum == 0) return;
  220.     PutString("");
  221.     PutString("End get group array article range one a time operation.");
  222. }
  223.  
  224.  
  225.  
  226. /*----------------------------------------------------------------------------
  227.     LogNewsrcParseBegin 
  228.     
  229.     Log the start of a newsrc parse.
  230.     
  231.     Entry:    newsrc = handle to newsrc with 0 terminator.
  232. ----------------------------------------------------------------------------*/
  233.  
  234. void LogNewsrcParseBegin (Handle newsrc)
  235. {
  236.     long len;
  237.  
  238.     if (gRefNum == 0) return;
  239.     PutString("");
  240.     PutString("Begin newsrc parse.");
  241.     len = GetHandleSize(newsrc) - 1;
  242.     HLock(newsrc);
  243.     FSWrite(gRefNum, &len, *newsrc);
  244.     HUnlock(newsrc);
  245.     PutString("");
  246. }
  247.  
  248.  
  249.  
  250. /*----------------------------------------------------------------------------
  251.     LogNewsrcGroupNameTooLong 
  252.     
  253.     Log a newsrc group name too long error.
  254.     
  255.     Entry:    groupName = group name.
  256. ----------------------------------------------------------------------------*/
  257.  
  258. void LogNewsrcGroupNameTooLong (char *groupName)
  259. {
  260.     char msg[512];
  261.     
  262.     if (gRefNum == 0) return;
  263.     sprintf(msg, "Group name too long: %s", groupName);
  264.     PutString(msg);
  265. }
  266.  
  267.  
  268.  
  269. /*----------------------------------------------------------------------------
  270.     LogNewsrcGroupNotInFullGroupList 
  271.     
  272.     Log a newsrc group not in full group list error.
  273.     
  274.     Entry:    groupName = group name.
  275. ----------------------------------------------------------------------------*/
  276.  
  277. void LogNewsrcGroupNotInFullGroupList (char *groupName)
  278. {
  279.     char msg[512];
  280.     
  281.     if (gRefNum == 0) return;
  282.     sprintf(msg, "Group not in full group list: %s", groupName);
  283.     PutString(msg);
  284. }
  285.  
  286.  
  287.  
  288. /*----------------------------------------------------------------------------
  289.     LogNewsrcNoGroupName 
  290.     
  291.     Log a newsrc no group name error.
  292. ----------------------------------------------------------------------------*/
  293.  
  294. void LogNewsrcNoGroupName (void)
  295. {
  296.     if (gRefNum == 0) return;
  297.     PutString("Missing group name");
  298. }
  299.  
  300.  
  301.  
  302. /*----------------------------------------------------------------------------
  303.     LogNewsrcReadListSyntaxError 
  304.     
  305.     Log a newsrc group article read list syntax error.
  306.     
  307.     Entry:    groupName = group name.
  308. ----------------------------------------------------------------------------*/
  309.  
  310. void LogNewsrcReadListSyntaxError (char *groupName)
  311. {
  312.     char msg[512];
  313.     
  314.     if (gRefNum == 0) return;
  315.     sprintf(msg, "Syntax error in article read list: %s", groupName);
  316.     PutString(msg);
  317. }
  318.  
  319.  
  320.  
  321. /*----------------------------------------------------------------------------
  322.     LogNewsrcSubscribedOK 
  323.     
  324.     Log a newsrc group subscribed OK.
  325.     
  326.     Entry:    groupName = group name.
  327. ----------------------------------------------------------------------------*/
  328.  
  329. void LogNewsrcSubscribedOK (char *groupName)
  330. {
  331.     char msg[512];
  332.     
  333.     if (gRefNum == 0) return;
  334.     sprintf(msg, "Subscribed, parsed OK: %s", groupName);
  335.     PutString(msg);
  336. }
  337.  
  338.  
  339.  
  340. /*----------------------------------------------------------------------------
  341.     LogNewsrcUnsubscribedOK 
  342.     
  343.     Log a newsrc unsubscribed group.
  344.     
  345.     Entry:    groupName = group name.
  346. ----------------------------------------------------------------------------*/
  347.  
  348. void LogNewsrcUnsubscribedOK (char *groupName)
  349. {
  350.     char msg[512];
  351.     
  352.     if (gRefNum == 0) return;
  353.     sprintf(msg, "Unsubscribed, parsed OK: %s", groupName);
  354.     PutString(msg);
  355. }
  356.  
  357.  
  358.  
  359. /*----------------------------------------------------------------------------
  360.     LogNewsrcNoColonOrBang 
  361.     
  362.     Log a newsrc group no colon or bang syntax error.
  363.     
  364.     Entry:    groupName = group name.
  365. ----------------------------------------------------------------------------*/
  366.  
  367. void LogNewsrcNoColonOrBang (char *groupName)
  368. {
  369.     char msg[512];
  370.     
  371.     if (gRefNum == 0) return;
  372.     sprintf(msg, "No ':' or '!' following group name: %s", groupName);
  373.     PutString(msg);
  374. }
  375.  
  376.  
  377.  
  378. /*----------------------------------------------------------------------------
  379.     LogNewsrcGroupNotOnServer 
  380.     
  381.     Log a newsrc group not on server error.
  382.     
  383.     Entry:    groupName = group name.
  384. ----------------------------------------------------------------------------*/
  385.  
  386. void LogNewsrcGroupNotOnServer (char *groupName)
  387. {
  388.     char msg[512];
  389.     
  390.     if (gRefNum == 0) return;
  391.     sprintf(msg, "Group not on server: %s", groupName);
  392.     PutString(msg);
  393. }
  394.  
  395.  
  396.  
  397. /*----------------------------------------------------------------------------
  398.     DumpUserGroupArray 
  399.     
  400.     Dump a user group array to the log file.
  401.     
  402.     Entry:    userGroupArray = handle to user group array.
  403.             numUserGroups = number of groups in array.
  404.             unsubscribed = handle to saved unsubscribed groups.
  405. ----------------------------------------------------------------------------*/
  406.  
  407. static void DumpUserGroupArray (TGroup **userGroupArray, short numUserGroups, Handle unsubscribed)
  408. {
  409.     char msg[512];
  410.     TGroup theGroup;
  411.     short i;
  412.     TUnread **unread;
  413.     long len;
  414.  
  415.     if (gRefNum == 0) return;
  416.     sprintf(msg, "%d groups in user group list.", numUserGroups);
  417.     HLock(gGroupNames);
  418.     PutString(msg);
  419.     for (i = 0; i < numUserGroups; i++) {
  420.         theGroup = (*userGroupArray)[i];
  421.         sprintf(msg, "%s: firstMess = %ld, lastMess = %ld, numUnread = %ld", 
  422.             *gGroupNames + theGroup.nameOffset, theGroup.firstMess,
  423.             theGroup.lastMess, theGroup.numUnread);
  424.         PutString(msg);
  425.         PutString("   Unread list:");
  426.         for (unread = theGroup.unread; unread != nil; unread = (**unread).next) {
  427.             sprintf(msg, "      %ld-%ld", (**unread).firstUnread, (**unread).lastUnread);
  428.             PutString(msg);
  429.         }
  430.     }
  431.     HUnlock(gGroupNames);
  432.     len = GetHandleSize(unsubscribed);
  433.     if (len > 0) {
  434.         PutString("Unsubscribed groups:");
  435.         HLock(unsubscribed);
  436.         FSWrite(gRefNum, &len, *unsubscribed);
  437.         HUnlock(unsubscribed);
  438.     }
  439. }
  440.  
  441.  
  442.  
  443. /*----------------------------------------------------------------------------
  444.     LogNewsrcParseEnd 
  445.     
  446.     Log the end of a newsrc parse.
  447.     
  448.     Entry:    userGroupArray = handle to user group array.
  449.             numUserGroups = number of groups in array.
  450.             unsubscribed = handle to saved unsubscribed groups.
  451. ----------------------------------------------------------------------------*/
  452.  
  453. void LogNewsrcParseEnd (TGroup **userGroupArray, short numUserGroups, Handle unsubscribed)
  454. {
  455.     if (gRefNum == 0) return;
  456.     PutString("");
  457.     PutString("End of newsrc parse.");
  458.     DumpUserGroupArray(userGroupArray, numUserGroups, unsubscribed);
  459. }
  460.  
  461.  
  462.  
  463. /*----------------------------------------------------------------------------
  464.     LogNewsrcWriteBegin 
  465.     
  466.     Log the beginning of a newsrc write.
  467.     
  468.     Entry:    userGroupArray = handle to user group array.
  469.             numUserGroups = number of groups in array.
  470.             unsubscribed = handle to saved unsubscribed groups.
  471. ----------------------------------------------------------------------------*/
  472.  
  473. void LogNewsrcWriteBegin (TGroup **userGroupArray, short numUserGroups, Handle unsubscribed)
  474. {
  475.     if (gRefNum == 0) return;
  476.     PutString("");
  477.     PutString("Begin newsrc write.");
  478.     DumpUserGroupArray(userGroupArray, numUserGroups, unsubscribed);
  479. }
  480.  
  481.  
  482.  
  483. /*----------------------------------------------------------------------------
  484.     LogNewsrcWriteEnd 
  485.     
  486.     Log the end of a newsrc parse.
  487.     
  488.     Entry:    newsrc = handle to newsrc.
  489. ----------------------------------------------------------------------------*/
  490.  
  491. void LogNewsrcWriteEnd (Handle newsrc)
  492. {
  493.     long len;
  494.  
  495.     if (gRefNum == 0) return;
  496.     PutString("");
  497.     PutString("End newsrc write.");
  498.     len = GetHandleSize(newsrc);
  499.     HLock(newsrc);
  500.     FSWrite(gRefNum, &len, *newsrc);
  501.     HUnlock(newsrc);
  502. }
  503.  
  504.  
  505.  
  506. /*----------------------------------------------------------------------------
  507.     LogTestBatchGroupCommandsBegin 
  508.     
  509.     Log the beginning of a batch group commands test.
  510. ----------------------------------------------------------------------------*/
  511.  
  512. void LogTestBatchGroupCommandsBegin (void)
  513. {
  514.     if (gRefNum == 0) return;
  515.     PutString("");
  516.     PutString("Begin batch group commands test.");
  517. }
  518.  
  519.  
  520.  
  521. /*----------------------------------------------------------------------------
  522.     LogTestBatchGroupCommandsEnd 
  523.     
  524.     Log the end of a batch group commands test.
  525.     
  526.     Entry:    response = pointer to response text.
  527.             respLen = length of response text.
  528.             serverSupportsBatchGroupCommands = result of test.
  529. ----------------------------------------------------------------------------*/
  530.  
  531. void LogTestBatchGroupCommandsEnd (char *response, unsigned short respLen, 
  532.     Boolean serverSupportsBatchGroupCommands)
  533. {
  534.     Handle tmpHandle;
  535.     long len;
  536.     CStr255 msg;
  537.  
  538.     if (gRefNum == 0) return;
  539.     PutString("");
  540.     sprintf(msg, "End of batch group commands test: gServerSupportsBatchGroupCommands = %s",
  541.         serverSupportsBatchGroupCommands ? "true" : "false");
  542.     PutString(msg);
  543.     PtrToHand(response, &tmpHandle, respLen);
  544.     Munge(tmpHandle);
  545.     len = GetHandleSize(tmpHandle);
  546.     HLock(tmpHandle);
  547.     FSWrite(gRefNum, &len, *tmpHandle);
  548.     MyDisposHandle(tmpHandle);
  549. }
  550.  
  551.  
  552.  
  553. /*----------------------------------------------------------------------------
  554.     LogHeaders 
  555.     
  556.     Log a server response to an XHDR or XPAT command.
  557.     
  558.     Entry:    response = pointer to response text.
  559.             respLen = length of response text.
  560. ----------------------------------------------------------------------------*/
  561.  
  562. void LogHeaders (char *response, long respLen)
  563. {
  564.     Handle tmpHandle;
  565.     long len;
  566.     
  567.     if (gRefNum == 0) return;
  568.     PutString("");
  569.     PutString("Headers received from server.");
  570.     PtrToHand(response, &tmpHandle, respLen);
  571.     Munge(tmpHandle);
  572.     len = GetHandleSize(tmpHandle);
  573.     HLock(tmpHandle);
  574.     FSWrite(gRefNum, &len, *tmpHandle);
  575.     MyDisposHandle(tmpHandle);
  576. }
  577.  
  578.  
  579.  
  580. /*----------------------------------------------------------------------------
  581.     LogMailMessage 
  582.     
  583.     Log a mail message.
  584.     
  585.     Entry:    text = pointer to message text.
  586.             textLen = length of message text.
  587. ----------------------------------------------------------------------------*/
  588.  
  589. void LogMailMessage (char *text, long textLen)
  590. {
  591.     Handle tmpHandle;
  592.     long len;
  593.     
  594.     if (gRefNum == 0) return;
  595.     PutString("");
  596.     PutString("Mail message just before sending to SMTP server.");
  597.     PtrToHand(text, &tmpHandle, textLen);
  598.     Munge(tmpHandle);
  599.     len = GetHandleSize(tmpHandle);
  600.     HLock(tmpHandle);
  601.     FSWrite(gRefNum, &len, *tmpHandle);
  602.     MyDisposHandle(tmpHandle);
  603.     PutString("");
  604. }
  605.  
  606.  
  607.  
  608. /*----------------------------------------------------------------------------
  609.     OpenLogFile 
  610.     
  611.     Opens the log file.
  612. ----------------------------------------------------------------------------*/
  613.  
  614. void OpenLogFile (void)
  615. {
  616.     FCBPBRec pBlock;
  617.     FSSpec logFile;
  618.     OSErr err;
  619.     Handle vers1Resource;
  620.     Str255 versStr;
  621.     CStr255 str;
  622.     
  623.     if (gRefNum != 0) return;
  624.     pBlock.ioNamePtr = nil;
  625.     pBlock.ioVRefNum = 0;
  626.     pBlock.ioRefNum = CurApRefNum;
  627.     pBlock.ioFCBIndx = 0;
  628.     err = PBGetFCBInfo(&pBlock, false);
  629.     if (err != noErr) goto exit;
  630.     err = FSMakeFSSpec(pBlock.ioFCBVRefNum, pBlock.ioFCBParID, 
  631.         "\pNewsWatcher Log", &logFile);
  632.     if (err != noErr && err != fnfErr) goto exit;
  633.     err = FSpOpenDF(&logFile, fsRdWrPerm, &gRefNum);
  634.     if (err == fnfErr) {
  635.         err = FSpCreate(&logFile, gPrefs.textCreator, 'TEXT', smSystemScript);
  636.         if (err != noErr) goto exit;
  637.         err = FSpOpenDF(&logFile, fsRdWrPerm, &gRefNum);
  638.     }
  639.     if (err != noErr) goto exit;
  640.     err = SetEOF(gRefNum, 0);
  641.     if (err != noErr) goto exit;
  642.     PutString("");
  643.     PutString("------------------------------------------------------------------------------");
  644.     PutString("");
  645.     PutString("Log opened.");
  646.     vers1Resource = GetResource('vers', 1);
  647.     pstrcpy(versStr, (StringPtr)*vers1Resource+6);
  648.     sprintf(str, "NewsWatcher version %#s", versStr);
  649.     PutString(str);
  650.     return;
  651.     
  652. exit:
  653.     if (gRefNum != 0) FSClose(gRefNum);
  654.     gRefNum = 0;
  655.     ErrorMessage("Could not open log file. No log will be written.");
  656. }
  657.  
  658.  
  659.  
  660. /*----------------------------------------------------------------------------
  661.     CloseLogFile 
  662.     
  663.     Closes the log file.
  664. ----------------------------------------------------------------------------*/
  665.  
  666. void CloseLogFile (void)
  667. {
  668.     if (gRefNum == 0) return;
  669.     PutString("");
  670.     PutString("Log closed.");
  671.     FSClose(gRefNum);
  672.     gRefNum = 0;
  673. }